package com.shop.cereshop.app.dl.logic.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.dl.CerePlatformUserPointMapper;
import com.shop.cereshop.app.dao.dl.CereShopProductMapper;
import com.shop.cereshop.app.dl.logic.domain.CereProductSku;
import com.shop.cereshop.app.dl.logic.domain.CereShopProduct;
import com.shop.cereshop.app.dl.logic.service.DlShopProductService;
import com.shop.cereshop.app.dl.logic.utils.ResultInfo;
import com.shop.cereshop.app.dl.logic.vo.Commodity;
import com.shop.cereshop.app.redis.service.api.SetRedisService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: dlshop
 * 描述：
 * @author: 我好帅
 * @create: 2021-06-16 10:57
 **/
@Service
@Transactional
public class DlShopProductServiceImpl implements DlShopProductService {

    @Value("${passPersonnel}")
    private String passPersonnel;
    @Autowired
    private CereShopProductMapper cereShopProductMapper;
    @Autowired
    private SetRedisService setRedisService;
    @Autowired
    private CerePlatformUserPointMapper cerePlatformUserPointMapper;

    @Override
    public ResultInfo<?> getCommodityByType(Integer page, Integer row, Integer classifyId, String content, CereBuyerUser user) {
//        if (content != null && !content.equals("")) {
//            setRedisService.sSet("searchHistory", content);
//        }

        //
        String[] split = passPersonnel.split(",");
        //判断是否是测试人员
        boolean flag = false;
        PageHelper.startPage(page, row);
        for (String s : split) {
            if (user != null && s.equals(user.getPhone())) {
                flag = true;
                break;
            }
        }

        List<CereShopProduct> list;
        if(flag && classifyId == 6){
            //如果通过人员有并且是集市,则能查询到测试商品,否则普通查询
            list = cereShopProductMapper.getCommodityByTypeAndTest(classifyId, content);
        }else{
            //根据类型序号(和搜索内容)查询该类所有商品
            list = cereShopProductMapper.getCommodityByType(classifyId, content);
        }
        List<Commodity> list1 = new ArrayList<>();
        for (CereShopProduct c : list) {
            Commodity co = new Commodity();
            CereProductSku sku = c.getProductSku();
            co.setSkuId(sku.getSkuId());
            co.setShopId(c.getShopId());

            if (c.getProductNo() != null) {
                co.setProductNo(c.getProductNo());
            }
            co.setClassifyId(c.getClassifyId());
            co.setProductId(c.getProductId());
            //设置旅游线路
            co.setTouristRoute(c.getTouristRoute());
            //商品图片--商品表关联商品图片表(1个商品对多个规格的图片,暂定1, 这里默认取第一个)
            if (c.getProductImages().size() > 0) {
                co.setImage(c.getProductImages().get(0).getProductImage());
            }
            co.setProductName(c.getProductName());
            if (classifyId == 7) {
                //如果是积分商城(类型7) 原价*10=积分
                co.setPrice(sku.getPrice().multiply(new BigDecimal(10)));
            } else {
                co.setOriginalPrice(sku.getOriginalPrice());
                co.setPrice(sku.getPrice());
            }
            Integer sold = cereShopProductMapper.getSoldOut(c.getProductId());
//            if (c.getProductNo()!=null&&c.getProductNo()<=0){
//                //总数-剩余==已售
//                int sold = sku.getTotal() - sku.getStockNumber();
            if(sold == null){
                sold = 0;
            }
                co.setSoldOut(sold);
//            }
            list1.add(co);
        }
        return new ResultInfo<>(new PageInfo<>(list1));
    }

    @Override
    public ResultInfo<?> getCommodityByMark(Integer page, Integer row) {
        PageHelper.startPage(page, row);
        List<CereShopProduct> list = cereShopProductMapper.getCommodityByMark();
        List<Commodity> list1 = new ArrayList<>();
        for (CereShopProduct c : list) {
            Commodity co = new Commodity();
            co.setShopId(c.getShopId());
            CereProductSku sku = c.getProductSku();
            co.setProductId(c.getProductId());
            co.setSkuId(sku.getSkuId());
            //赋值票务标记, 有值就是票, 没值就是商品
            if (c.getProductNo() != null) {
                co.setProductNo(c.getProductNo());
            }
            co.setClassifyId(c.getClassifyId());
            //赋值商品图片--商品表关联商品图片表(product_image)(1个商品(shop_product表)对多个规格的图片,暂定1, 这里默认取第一个)
            if (c.getProductImages().size() > 0) {
                co.setImage(c.getProductImages().get(0).getProductImage());
            }
            co.setProductName(c.getProductName());
            if (c.getClassifyId() == 7) {
                //如果是积分商城(类型7) 原价*10=积分
                co.setPrice(sku.getPrice().multiply(new BigDecimal(10)));
            } else {
                co.setOriginalPrice(sku.getOriginalPrice());
                co.setPrice(sku.getPrice());
            }
            Integer sold = cereShopProductMapper.getSoldOut(c.getProductId());
//            if (c.getProductNo()==null){
//                //总数-剩余==已售
//                int sold = sku.getTotal() - sku.getStockNumber();
            if(sold == null){
                sold = 0;
            }
                co.setSoldOut(sold);
//            }
            list1.add(co);
        }
        PageInfo pageInfo = new PageInfo(list1);
        return new ResultInfo(pageInfo);
    }
}
